package util;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/*
 * 文件目录操作方法
 * 参考：https://blog.csdn.net/brushli/article/details/12356695
 */
public class FileUtil {
	/**
	 * 创建文件
	 * @param filePath 文件路径
	 * @return 创建是否成功
	 */
	public static boolean createFile(String filePath){
		boolean result = false;
		File file = new File(filePath);
		if(!file.exists()){
			try {
				result = file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return result;
	}
	/**
	 * 创建文件夹
	 * @param directory
	 * @return
	 */
	public static boolean createDirectory(String directory){
		boolean result = false;
		File file = new File(directory);
		if(!file.exists()){
			result = file.mkdirs();
		}
		
		return result;
	}
	/**
	 * 删除文件
	 * @param filePath
	 * @return
	 */
	public static boolean deleteFile(String filePath){
		boolean result = false;
		File file = new File(filePath);
		if(file.exists() && file.isFile()){
			result = file.delete();
		}
		
		return result;
	}
	/**
	 * 删除文件夹，
	 * 递归删除文件夹下面的子文件和文件夹
	 * @param filePath
	 */
	public static void deleteDirectory(String filePath){
		File file = new File(filePath);
		if(!file.exists()){
			return;
		}
		
		if(file.isFile()){
			file.delete();
		}else if(file.isDirectory()){
			File[] files = file.listFiles();
			for (File myfile : files) {
				deleteDirectory(filePath + "/" + myfile.getName());
			}
			
			file.delete();
		}
	}
	/**
	 * 读文件，以字节为单位读取文件，常用于读二进制文件，如图片、声音、影像等文件
	 * @param filePath
	 * @return
	 */
	public static String readFileByBytes(String filePath){
		File file = new File(filePath);
		if(!file.exists() || !file.isFile()){
			return null;
		}
		
		StringBuffer content = new StringBuffer();
		
		try {
			byte[] temp = new byte[1024];
			FileInputStream fileInputStream = new FileInputStream(file);
			while(fileInputStream.read(temp) != -1){
				content.append(new String(temp));
				temp = new byte[1024];
			}
			
			fileInputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return content.toString();
	}
	/**
	 * 以字符为单位读取文件，常用于读文本，数字等类型的文件，支持读取中文
	 * @param filePath 文件路径
	 * @return
	 */
	public static String readFileByChars(String filePath){
		File file = new File(filePath);
		if(!file.exists() || !file.isFile()){
			return null;
		}
		
		StringBuffer content = new StringBuffer();
		try {
			char[] temp = new char[1024];
			FileInputStream fileInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
			while(inputStreamReader.read(temp) != -1){
				content.append(new String(temp));
				temp = new char[1024];
			}
			
			fileInputStream.close();
			inputStreamReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return content.toString();
	}
	
	
	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 * @param filePath
	 * @return
	 */
	public static List<String> readFileByLines(String filePath){
		File file = new File(filePath);
		if(!file.exists() || !file.isFile()){
			return null;
		}
		
		List<String> content = new ArrayList<String>();
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
			BufferedReader reader = new BufferedReader(inputStreamReader);
			String lineContent = "";
			while ((lineContent = reader.readLine()) != null) {
				content.add(lineContent);
				System.out.println(lineContent);
			}
			
			fileInputStream.close();
			inputStreamReader.close();
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return content;
	}
	
	/*
	 *写文件,字符串写入文件的几个类中，FileWriter效率最高，BufferedOutputStream次之，FileOutputStream最差。
	 */
	
	/**
	 * 通过FileOutputStream写入文件
	 * @param filePath
	 * @param content
	 * @throws IOException
	 */
	public static void writeFileByFileOutputStream(String filePath, String content) throws IOException{
		File file = new File(filePath);
		synchronized (file) {
			FileOutputStream fos = new FileOutputStream(filePath);
			fos.write(content.getBytes("GBK"));
			fos.close();
		}
	}
	/**
	 * 通过BufferedOutputStream写入文件
	 * @param filePath
	 * @param content
	 * @throws IOException
	 */
	public static void writeFileByBufferedOutputStream(String filePath, String content) throws IOException{
		File file = new File(filePath);
		synchronized (file) {
			BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filePath));
			fos.write(content.getBytes("GBK"));
			fos.flush();
			fos.close();
		}
	}
	/**
	 * 通过FileWriter将字符串写入文件
	 * @param filePath
	 * @param content
	 * @throws IOException
	 */
	public static void writeFileByFileWriter(String filePath, String content) throws IOException{
		File file = new File(filePath);
		synchronized (file) {
			FileWriter fw = new FileWriter(filePath);
			fw.write(content);
			fw.close();
		}
	}
	/**
	 * 本地文件路径转URL
	 * Pass a local file in to URL in Java：
	 * https://stackoverflow.com/questions/6098472/pass-a-local-file-in-to-url-in-java
	 * 互转：https://blog.csdn.net/linjx2004/article/details/5608909
	 * @param filePath
	 * @return
	 */
	URL FilePathToUrl(String filePath) {
		URL url = null;
		try {
			url =  new File(filePath).toURI().toURL();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return url;
	}
}
